package helper

import (
	"encoding/json"
	"fmt"
	"net/http"
)

func ResponseJSON(w http.ResponseWriter, statusCode int, v interface{}) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(statusCode)
	return json.NewEncoder(w).Encode(&v)
}

func ResponseErrJSON(w http.ResponseWriter, statusCode int, err error) error {
	d := map[string]string{
		"error": fmt.Sprintf("%v", err),
	}
	fmt.Println("d:", d)
	return ResponseJSON(w, statusCode, d)
}

func ResponseText(w http.ResponseWriter, statusCode int, v interface{}) error {
	w.Header().Set("Content-Type", "text/plain")
	w.WriteHeader(statusCode)
	_, err := w.Write([]byte(v.(string)))
	return err
}

func ResponseErrText(w http.ResponseWriter, statusCode int, err error) error {
	d := fmt.Sprintf("error: %v", err)
	return ResponseText(w, statusCode, d)
}
